"use strict";
var $GLOBAL_USER = 'developer';
/* Controllers */
app.controller("ticketListController", ['$scope', '$location', '$rootScope', '$routeParams', 'ticketListService', '$q', function ($scope, $location, $rootScope, $routeParams, ticketListService, $q) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.type = $routeParams.type;
    if(vm.p.type=='rg') {
        vm.p.resolvegroupId = $routeParams.id;
    }else {
         vm.p.eventId = $routeParams.id; //  获取事件Id
    }
//    vm.p.ownerId = $GLOBAL_USER;
//    if(vm.p.ownerId=='developer') {
//        vm.p.ownerId = 'all';
//    }
    vm.p.ownerId = 'all';
    vm.p.interval = '7d';

    $scope.loading = false;
    var errorFn = function (result) {
        alert(result);
    };

    $scope.toggleInterval = function (interval) {
        var vm = $scope.vm;
        vm.p.interval = interval;
    }

    $scope.listTicket = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = vm.p;

        var procFn = function (response) {
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        ticketListService.listTicket(params, procFn, errorFn);
    };

    $scope.toBatchDeal = function () {
        var ids = getIdSelections()
        if (ids.length <= 0) {
            $scope.loading = true
            $scope.vm.p.showNoSelect = true
        } else {
            $scope.loading = false
            $scope.vm.p.showNoSelect = false
        }
        $('#myModal').modal('show')
    }

    $scope.ok = function () {
        $scope.loading = true
        let ids = getIdSelections()
        $scope.vm.p.ids = ids

        let params = {};
        params = $scope.vm.p
        var procFn = function (response) {
            $scope.loading = false
            var data = response.data;
            if (data == null) return;
            $('#myModal').modal('hide')
            if (response.success && response.data) {
                alert("批量处理成功")
            } else {
                alert(response.msg);
            }
            $('#myModal').modal('hide')
        };
        ticketListService.batchUpdateStatus(params, procFn, errorFn);
    }

    function getIdSelections() {
        return $.map($('#list_table').bootstrapTable('getSelections'), function (row) {
            return row.id
        })
    }

    $scope.pingDetail = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            vm.p.requester = data[0].userName
        };
        return ticketListService.pingDetail(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'location');
        // $resultTable.bootstrapTable('hideColumn', 'reportDate');
        $resultTable.bootstrapTable('hideColumn', 'type');
//        $resultTable.bootstrapTable('hideColumn', 'noteReason');

        $resultTable.bootstrapTable('hideColumn', 'category');
        $resultTable.bootstrapTable('hideColumn', 'dealUser');
        $resultTable.bootstrapTable('hideColumn', 'isNotifyRg');
        $resultTable.bootstrapTable('hideColumn', 'duration');
        $resultTable.bootstrapTable('hideColumn', 'updateTime');

        $q.all([$scope.pingDetail()]).then(function () {
            $scope.listTicket();
        })
    };

    $scope.initializeController();


}]);


/* Services */
app.service('ticketListService', ['ajaxService', function (ajaxService) {

    this.listTicket = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/ticket/listTicket.do", successFunction, errorFunction);
    };

    this.batchUpdateStatus = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/ticket/batchUpdateStatus.do", successFunction, errorFunction);
    };

    this.pingDetail = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/aaa/pingDetail.do", successFunction, errorFunction);
    };
}]);

function ticketIDOptFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/ticket/add/', value, '" target="_blank">', "<u>修改</u>", '</a>',
        '<a href="#/star/onduty/ticket/deal/', value, '" target="_blank">', " <u>处理</u>", '</a>'
    ].join('');
}

function severityFmt(value, row, index) {
    if (value == null) return null;
    var ret = '';
    value = value.toLowerCase();
    if (value == 'critical') {
        ret = formatColorCtx('严重的', 'bright red');
    } else if (value == 'urgent') {
        ret = formatColorCtx('紧急的', 'red');
    } else if (value == 'high') {
        ret = formatColorCtx('高的', 'orange');
    } else if (value == 'normal') {
        ret = formatColorCtx('一般的', '#000000');
    } else if (value == 'low') {
        ret = formatColorCtx('低的', 'light green');
    } else {
        ret = formatColorCtx('未知:' + value, 'grey');
    }
    return ret;
}
function ticketStatusFmt(value, row, index) {
    if (value == null) return null;
    var ret = '';
    value = value.toLowerCase();
    if (value == 'opened') {// opened
        ret = formatColorCtx('未处理', 'bright red');
    } else if (value == 'processing') {// processing
        ret = formatColorCtx('处理中', 'red');
    } else if (value == 'resolved') {//resolved
        ret = formatColorCtx('待验证', 'orange');
    } else if (value == 'checking') {//checking
        ret = formatColorCtx('验证中', 'light green');
    } else if (value == 'invalid') {// invalid
        ret = formatColorCtx('已废除', '#000000');
    } else if (value == 'misreport') {// invalid
        ret = formatColorCtx('误报', '#000000');
    } else if (value == 'closed') {// invalid
        ret = formatColorCtx('已关闭', '#000000');
    } else {
        ret = formatColorCtx('未知:' + value, 'grey');
    }
    return ret;
}

// bug, account, host, service, policy
function ticketTypeFmt(value, row, index) {
    if (value == null) return null;
    var ret = '';
    value = value.toLowerCase();
    if (value == 'bug') {
        ret = formatColorCtx('软件缺陷', 'bright red');
    } else if (value == 'account') {
        ret = formatColorCtx('账户权限', 'red');
    } else if (value == 'host') {
        ret = formatColorCtx('主机硬件', 'orange');
    } else if (value == 'service') {
        ret = formatColorCtx('软件服务', '#000000');
    } else if (value == 'policy') {
        ret = formatColorCtx('应用策略', 'light green');
    } else {
        ret = formatColorCtx('未知:' + value, 'grey');
    }
    return ret;
}


function noteFmt(value, row, index) {
    if (value == null) return '-';
    if (row.noteTag && value) {
        // not fix（不做修复）, duplicated（已重复）, not issue（不是问题）, by design（按设计）, not recur(未重现)
        return row.noteTag + ':' + value;
    } else if (row.noteTag) {
        return row.noteTag;
    } else if (value) {
        return value;
    } else {
        return '-';
    }
}

function descriptionFmt(value, row, index) {
    if (value == null) {
        return '-';
    }
    var items = value.split(/。|，|,/);
    if (items.length<2) {
        return value;
    }
    return [
            items[0], ',', items[1]
    ].join('');
}

function requesterFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var items = value.split(',');
    if(items[0]=='user') {
        return items[2]+'('+items[1]+')';
    } else {
        return [
            '<a href="#/star/onduty/agent_add/id/', items[1], '" target="_blank">', "<u>",items[2],"</u>", '</a>'
        ].join('');
    }
}

function ctiIdFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/cti_add/id/', value, '" target="_blank">', "<u>",row.ctiName,"</u>", '</a>'
    ].join('');
}



function resolvegroupFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="#/star/onduty/resolvegroup_add/code/', row.resolvegroupId, '" target="_blank">', "<u>",value,"</u>", '</a>',
//        '(',row.resolvegroupId,')'
    ].join('');
}

